<div class="col-md-12 pull-left row">
    <form class="form-inline" id="order-form">
        <label>预约状态</label>
        <select name="status">
            <option value="0" selected="selected">未处理</option>
            <option value="1">已处理</option>
        </select>
        <label>预约日期</label>
        <input type="date" name="start_date">
        <label>至</label>
        <input type="date" name="end_date">
        <label>推荐人</label>
        <input type="text" name="referrer">
        <input type="button" id="order-search" value="搜索">
    </form>
    <div class="col-md-2 pull-right row" style="height: 100%;margin-bottom: 5px">
        <input type="button" id="refresh" class="btn btn-success" value="开启自动刷新">
        <span id="time_count"></span>
    </div>
</div>
<div class="col-md-12" id="order_table">
    <div class="table-condensed" style="overflow-y: auto;overflow-x: auto;width: 100%;height: 700px">
        <table class="table table-hover table-striped table-bordered text-center">
            <tr>
                <th class="text-center">序号</th>
                <th class="text-center">姓名</th>
                <th class="text-center">手机号</th>
                <th class="text-center">省份证号码</th>
                <th class="text-center">装机住址</th>
                <th class="text-center">推荐人</th>
                <th class="text-center">预约日期</th>
                <th class="text-center">套餐类型</th>
                <th class="text-center">套餐价格</th>
                <th class="text-center">状态</th>
                <th class="text-center">操作</th>
            </tr>
            <volist name="order_list" id="item">
                <tr>
                    <td>{$item['id']}</td>
                    <td style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis">{$item['name']}</td>
                    <td style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis">{$item['phone']}</td>
                    <td style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis">{$item['id_card']}</td>
                    <td style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis">{$item['address']}</td>
                    <td style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis">{$item['referrer']}</td>
                    <td>{$item['order_date']|date="Y-m-d",###}</td>
                    <td style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis">{$item['good_name']}</td>
                    <td>{$item['good_price']}</td>
                    <td>{$item['status']?'已处理':'未处理'}</td>
                    <td>
                        <if condition="$item['status'] eq 1">
                            <input type="button" disabled="disabled" style="text-decoration: none" value="处理">
                            <else />
                            <input type="button" name="deal" value="处理">
                        </if>
                        <input type="button" name="order_info" value="详情">
                        <input type="button" name="order_delete" style="color: darkred" value="删除">
                    </td>
                </tr>
            </volist>
        </table>
    </div>
</div>
<script>
    //处理订单
    $(document).ready(function () {
        $("input[name='deal']").click(function () {
            console.log('deal');
            var obj = $(this).parent().parent();
            var id = obj.children("td:first-child").text();
            console.log('id: '+id);
            $.ajax({
                url: '/admin.php/Order/preDoneOrder/id/'+id,
                method:'get',
                dataType:'json',
                success: function (res) {
                    if (res.code == 200) {
                        $("#order-page").empty();
                        $("#order-page").append(res.data);
                    }
                }
            });
        });
    });
    //查看详情
    $(document).ready(function () {
        $("input[name='order_info']").click(function () {
            var obj = $(this).parent().parent();
            var id = obj.children("td:first-child").text();
            console.log('id: '+id);
            $.ajax({
                url: '/admin.php/Order/getOrderInfo/id/'+id,
                method:'get',
                dataType:'json',
                success: function (res) {
                    if (res.code == 200) {
                        $("#order-page").empty();
                        $("#order-page").append(res.data);
                    }
                }
            });
        });
    });
    //查询预约订单
    $(document).ready(function () {
        $("#order-search").click(function () {
            console.log('search');
            var data = $("#order-form").serialize();
            console.log(typeof data,data);
            $.ajax({
                url: "/admin.php/Order/searchOrder",
                data: data,
                method: 'post',
                dataType: 'json',
                success: function (res) {
                    if (res.code == 200) {
                        $("#order_table").empty();
                        $("#order_table").append(res.data);
                    } else if(res.code == 100) {
                        alert("未搜索到结果，请更改条件重试 !");
                    } else {
                        alert("查询失败，请稍后重试 !");
                    }
                }
            });
        });
    });
    //删除
    $(document).ready(function () {
        $("input[name='order_delete']").click(function () {
            var obj = $(this).parent().parent();
            var id = obj.children("td:first-child").text();
            console.log('id: ' + id);
            var r = confirm('确认删除吗？');
            if (r == true) {
                $.ajax({
                    url: '/admin.php/order/deleteOrder/orderId/' + id,
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 200) {
                            alert("删除成功");
                            obj.hide(500);
                        } else {
                            alert('删除失败');
                        }
                    }
                });
            }
        });
    });
    //自动刷新未处理订单
    $(document).ready(function () {
        $("#refresh").click(function () {
            count = !count;
            if (count) {
                $("#refresh").val('停止自动刷新');
                $("#refresh").removeClass('btn-success');
                $("#refresh").addClass('btn-danger');
                $('body').everyTime('1s',refresh);
            } else {
                $("#refresh").val('开始自动刷新');
                $("#refresh").removeClass('btn-danger');
                $("#refresh").addClass('btn-success');
                $('body').stopTime();
            }
        });
    });
    //计时器
    function refresh() {
        $("#time_count").html('&nbsp;&nbsp;'+time);
        if (--time < 0) {
            time = 5;
            refresh_table();
        }
    }
    //表格刷新
    function refresh_table() {
        $.ajax({
            url: "/admin.php/Order/refresh",
            method: 'post',
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    $("#order_table").empty();
                    $("#order_table").append(res.data);
                }
            }
        });
    }
</script>